package com.jcj.pojoprotobuf;

import com.jcj.pojomessagepack.MessagePackDecoder;
import com.jcj.pojomessagepack.MessagePackEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

/**
 * 服务器端handler初始化配置类,在Channel注册到EventLoop后,对这个Channel添加一些初始化的Handler
 */
public class InitialzerServerPB extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();
        //protobuf的帧解码器,用于解决TCP黏包和拆包问题
        pipeline.addLast("framer Decoder", new ProtobufVarint32FrameDecoder());
        //设置我们的proto文件生成的实例,其实就是我们的目标java类
        pipeline.addLast("Protobuf decoder", new ProtobufDecoder(InformationProto.Information.getDefaultInstance()));
        //protobuf的编码器,用于解决TCP黏包和拆包问题
        pipeline.addLast("framer Encoder", new ProtobufVarint32LengthFieldPrepender());
        //protobuf方式编码器,用于解决TCP粘包和拆包问题
        pipeline.addLast("Protobuf encoder", new ProtobufEncoder());
        //自定义业务逻辑handler
        pipeline.addLast(new HandlerServerPB());

    }
}

















